<?php

namespace app\admin\model;

use think\Model;
use think\Validate;


/**
 * 基类模型
 * Class Base
 * @package app\admin\model
 */
class Base extends Model
{

    protected $after_limit;//分页大小
    protected $after_page;//分页大小
    protected $admin;//用户
    protected $substations;//当前分站列表
    protected $prefix = 'shop_';

    //初始化
    public function initialize(){

        //调用父类初始化
        parent::initialize();

        //获取分页信息
        $this->after_limit = session("after_limit");
        $this->after_page = session("after_page");

        //获取用户信息
        $this->admin = session("admin");

        //当前分站设置
        $this->substations = session("substations");
    }


    //检查数据是否符合规则
    public function checkData($data,$check){
        $is = true;
        $validate = new Validate($check);
        if (!$validate->check($data)) {
            $is = $validate->getError();
        }
        return $is;
    }

    /**
     * Author:Jack Wang
     * Time:2018-10-18 17:22:46
     * Describe:基类添加修改
     * @param $data
     * @param string $filed_id
     * @param int $id
     * @return bool|false|int
     */
    public function mySave($data,$filed_id = "id",$id = 0){
        $state = false;
        if(!empty($id) || !empty($data[$filed_id])){
            if(!empty($data[$filed_id])){
                //获取id，传入获取id参数为主没有传入直接获取data中的获取id
                $id = !empty($id) ? $id :$data[$filed_id];
                unset($data[$filed_id]);//修改时候移除menu_id
            }
            $state = $this->allowField(true)->isUpdate(true)->save($data,[$filed_id=>$id]);
        }else{
            $state = $this->allowField(true)->isUpdate(false)->data($data)->save();
        }
        return $state;
    }

    /**
     * Author:Jack Wang
     * Time:2018-10-19 11:53:50
     * Describe:基类多数据添加修改
     * @param $data
     * @param bool $isAdd
     * @return array|bool|false
     * @throws \Exception
     */
    public function mySaveAll($data,$isAdd = false){
        $state = false;
        if($isAdd){
            $state = $this->allowField(true)->saveAll($data,false);
        }else{
            $state = $this->allowField(true)->saveAll($data);
        }
        return $state;
    }

    //获取某个字段的值
    public function getFieldValue($field_where,$where,$get_field,$substation_id = 0){
        return $this
            ->where(function ($q)use ($substation_id){
                if(!empty($substation_id)){
                    $q->where("substation_id",$substation_id);
                }
            })
            ->where($field_where,$where)
            ->value($get_field);
    }

    /**
     * Author:Jack Wang
     * Time:2018-10-23 14:56:09
     * Describe:更新某个字段的值
     * @param $field_where
     * @param $where
     * @param $set_field
     * @param $value
     * @return int
     */
    public function setFieldValue($field_where,$where,$set_field,$value){
        return $this->where($field_where,$where)->setField($set_field,$value);
    }

    //检查字段值是否唯一
    public function checkFieldOnly($field_where,$where,$id_field,$id,$substation_id = 0){
        return $this->where($field_where,$where)->where($id_field,"neq",$id)->where(config("TableDeleteField"),0)
            ->where(function ($q)use ($substation_id){
                if(!empty($substation_id)){
                    $q->where("substation_id",$substation_id);
                }
            })
            ->count();
    }

    //根据主键检查字段值是否唯一
    public function checkPKFieldOnly($field_where,$where,$id,$substation_id = 0){
        return $this->checkFieldOnly($field_where,$where,$this->getPk(),$id,$substation_id);
    }

    //获取详细信息
    public function dataInfo($field_where,$where){
        return $this->where($field_where,$where)->find();
    }

    //根据主键获取详细信息
    public function PkInfo($pk_where){
        return $this->dataInfo($this->getPk(),$pk_where);
    }

    //删除
    public function del($field_where,$where,$is = false){
        if(!$is) {
            return $this->where($field_where, $where)->setField(config("TableDeleteField"), 1);
        }else{
            return $this->where($field_where, $where)->delete();
        }
    }

    //根据主键删除
    public function PKDel($where,$is = false){
        return $this->del($this->getPk(),$where,$is);
    }

    //获取条件个数
    public function getFieldCount($field_where,$where,$substation_id = 0){
        return $this
            ->where(function ($q)use ($substation_id){
                if(!empty($substation_id)){
                    $q->where("substation_id",$substation_id);
                }
            })
            ->where($field_where,$where)
            ->count();
    }


}